/* Exercise 6.4 Message test */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly #define SPEED 400 // Used to trim the delay loops #include "lcdlib.h" const unsigned char messages [] = { 'n','o','*', 'y','e','s','*', 'p','r','o','b','a','b','l','y','*', 'n','e','v','e','r','*', 'n','o','t',' ','l','i','k','e','l','y','*', 'n','o','t',' ','t','o','d','a','y','*', 'u','n','c','l','e','a','r','*', 'd','o','n','t',' ','b','e','t',' ','o','n',' ','i','t','*', 'p','e','r','h','a','p','s','*', 'o','u','t','l','o','o','k',' ','h','a','z','y','*', 'a','s','k',' ','a','g','a','i','n','*', 'i','t',' ','d','e','p','e','n','d','s','*', 0x00 }; const unsigned char banner1 [] = {'*','*','*','M','y','s','t','i', 'c', ' ','L','C','D','*','*','*',0x00} ; const unsigned char banner2 [] = {'*','*','P','r','e','s','s',' ','t','o',' ','a','s','k','*','*',0x00} ; unsigned char seed = 1; //Variables associated with random number generation //LFSR based algorithm for random number generation //exclusive or bit 4 with bit 7, shift the seed and add the new bit unsigned char random ( void ) { unsigned char top, mid ; if ( seed & 12 ) //get new input bit { mid = 1 ; } else { mid = 0 ; } if ( seed & 0x80 ) { top = 1 ; } else { top = 0 ; } seed = ( seed << 1 ) | ( mid ^ top ) ; return seed ; } //#define DEBUG void show_message ( unsigned char number ) { int pos = 0 ; while (number > 0) //find the start of the message { #ifdef DEBUG lcd_print_ch ( messages [pos] ) ; #endif if (messages [pos] == '*') { number = number - 1 ; } pos = pos + 1 ; //This is the line which is missing in the previous version } while ( messages [pos] != '*' ) //print the message { lcd_print_ch ( messages [pos] ) ; pos = pos + 1 ; } } void flicker ( void ) { unsigned char r = random () ; lcd_cursor ( r % 16, r / 128 ) ; lcd_print_ch ( 'A' + random () % 26 ) ; } //counts the messages in the message string, returns the result as a an integer unsigned char count_messages ( void ) { unsigned char i, count = 0 ; for (i = 0; messages[i] != 0; i = i + 1 ) { if (messages[i] == '*') { count = count + 1; } } return count; } void big_delay ( int size ) { int i, j ; for (i = 0; i < size; i = i + 1 ) { for (j = 0; j < SPEED; j = j + 1); } } void setup_hardware (void) { ANSELA = 0x00; //Put PORTA into Digital mode, defaults to analogue ANSELB = 0x00; //Put PORTB into Digital mode, defaults to analogue OPTION_REG = 0xC0; TRISA = 0x01 ; //bit 0 of PORTA for input everything else is output } //returns the debounced state of the bottom bit of PORTA unsigned char key ( void ) { unsigned char count = 0; unsigned char oldv, newv; oldv = PORTA & 0x01 ; while (count < 20) { newv = PORTA & 0x01 ; if (oldv == newv) { count++ ; } else { count = 0 ; oldv = newv ; } } return oldv ; } int main (void) { unsigned char i, lim; setup_hardware(); lcd_start (); lim = count_messages () ; while (1) //Loop forever { lcd_clear () ; lcd_print ( banner1 ) ; lcd_cursor ( 0, 1 ) ; lcd_print ( banner2 ) ; while ( key() == 0 ) random (); //wait for keypress while ( key () == 1 ) flicker (); //wait for key up lcd_clear (); show_message ( random () % lim ); big_delay ( 3000 ); } return 0; }